package com.teaching.Dao;

import com.teaching.Enity.Course;
import com.teaching.Enity.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.websocket.OnClose;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class CourseDaoAchieve implements CourseDao {

    private SqlSession sqlSession;
    private String resource = "mybatis.xml";

    @Override//从数据库获取老师教的课
    public List<Course> findTeacherCourse(int t_id) throws IOException{
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = sessionFactory.openSession();
        List<Course> courseList = sqlSession.selectList("findTeacherCourse",t_id);
        sqlSession.commit();
        sqlSession.close();
        return courseList;
    }

    @Override//从数据库获取全部课程
    public List<Course> findAllCourse() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = sessionFactory.openSession();
        List<Course> courseList = sqlSession.selectList("findAllCourse");
        sqlSession.commit();
        sqlSession.close();
        return courseList;
    }

    @Override//从数据库获取学生选的课
    public List<Course> findStudentCourse(int s_id) throws IOException{
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = sessionFactory.openSession();
        List<Course> courseList = sqlSession.selectList("findStudentCourse",s_id);
        sqlSession.commit();
        sqlSession.close();
        return courseList;
    }
}
